package service

import (
	"github.com/spf13/viper"
	"github.com/tebeka/selenium"
)

var (
	Config ServerConfig
)

type ServerConfig struct {
	SeleniumPath string
	Port         int
	Debug        bool
}

func InitConfig() {
	Config.SeleniumPath = viper.GetString("selenium.seleniumPath")
	Config.Port = viper.GetInt("selenium.port")
	Config.Debug = viper.GetBool("selenium.debug")
}

func StartSeleniumServer() (*selenium.Service, error) {
	InitConfig()
	//如果seleniumServer没有启动，就启动一个seleniumServer所需要的参数，可以为空，示例请参见https://github.com/tebeka/selenium/blob/master/example_test.go
	var opts []selenium.ServiceOption
	//opts := []selenium.ServiceOption{
	//    selenium.StartFrameBuffer(),           // Start an X frame buffer for the browser to run in.
	//    selenium.GeckoDriver(geckoDriverPath), // Specify the path to GeckoDriver in order to use Firefox.
	//}

	selenium.SetDebug(Config.Debug)
	service, err := selenium.NewChromeDriverService(Config.SeleniumPath, Config.Port, opts...)
	if nil != err {
		return nil, err
	}
	//注意这里，server关闭之后，chrome窗口也会关闭
	return service, nil
}
